/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-20 09:16:29
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-10-20 18:49:50
 */
import abiao_utils from 'abiao_utils';
import express from 'express';
import { signApi } from '../config/api.config';
import reward from '../controllers/reward.controller';
import user from '../controllers/user.contorller';
import { Code } from '../enum/code.enum';
import { FUNC_NAME } from '../enum/itemLogs.enum';
import Sign from '../models/sign/sign.model';
import utils from '../utils/utils';
const signRoute = express.Router();

/**
 * 执行签到
 */
signRoute.post(signApi.doSign, async (req, res) => {

  let { roleId, type, bqDay } = req.body;
  let userInfo: any = await user.getUserInfo(roleId);
  let SignDay: number = utils.dateMinus(userInfo.createTime.split(" ")[0], abiao_utils.date(2)) + 1; //签到第几天

  if (type == 1) {
    //当天签到
    let rewardInfo: any = await utils.get_id_json('sign/sign.json', SignDay, 'id');
    await Sign.updateOne({ roleId, id: SignDay }, { isReceive: true });
    await reward.sendItem(roleId, `签到第${SignDay}天`, [{ day: SignDay, type: reward.propName.diamond, num: rewardInfo.reward }], FUNC_NAME.SIGN_IN);
    let SignRes: any = await Sign.find({ roleId });
    userInfo = await user.getUserInfo(roleId);
    return res.json({ code: Code.Succ, message: "签到成功!", data: { SignRes, userInfo } });
  } else if (type == 2) {
    //补签
    let rewardInfo: any = await utils.get_id_json('sign/sign.json', bqDay, 'id');
    await Sign.updateOne({ roleId, id: bqDay }, { isReceive: true });
    await reward.sendItem(roleId, `补签第${bqDay}天`, [{ day: bqDay, type: reward.propName.diamond, num: rewardInfo.reward }], FUNC_NAME.SIGN_IN);
    let SignRes: any = await Sign.find({ roleId });
    userInfo = await user.getUserInfo(roleId);
    return res.json({ code: Code.Succ, message: "补签成功!", data: { SignRes, userInfo } });
  }
})

export default signRoute;
